/*
 * @Author: C.
 * @Date: 2021-12-23 10:48:22
 * @LastEditTime: 2021-12-27 12:58:52
 * @Description: file content
 */
import { genInstance } from "./propsHelp";
import Form from "../Form";
// 列表类
class List {
  // 数据集合
  listData = []
  // 请求参数
  form = {
  }
  formObj=null
  api=null
  // 是否是最后一页
  isLast = true
  loading = false
  formCopy = null
  constructor(listProps) {
    // this.form = form
    // this.formCopy = JSON.stringify(form)
    this.init(listProps)
  }
  init=(props)=>{
    genInstance(this,props)
  }
  // 设置form
  setForm=(form)=>{
    Object.assign(this.form,form)
  }
  // 获取集合
  getData = () => {
    // this.loading = true
    this.api.getData(this.form).then((res) => {
      this.isLast = this.form.PageSize > res.Items.length
      this.listData =
        this.form.CurrentPage == 1
          ? res.Items
          : [...this.listData, ...res.Items]
      this.loading = false
    })
  }
  // 刷新事件
  onRefresh = () => {
    this.form.CurrentPage = 1
    this.loading = true
    this.getData()
  }
  // 加载事件
  getMoreData = () => {
    if (this.isLast) return
    this.form.CurrentPage += 1
    this.getData()
  }
  rePageEvent = () => {
    let newForm = JSON.parse(this.formCopy)
    this.form.CurrentPage = newForm.CurrentPage
    this.form.PageSize = newForm.PageSize
    this.getData()
  }
}
export default List
